package com.lds.erp.common.vo;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hongsir
 * @apiNote 返回菜单帮助类
 * @date 2018/9/29 上午10:54
 */
public class LoginMenuVOHelper {

    public static List<LoginMenuVO> list2tree(List<LoginMenuVO> list) {
        List<LoginMenuVO> rootTrees = new ArrayList<>();
        for (LoginMenuVO tree : list) {
            // 根节点的上级id必须为空
            if (StringUtils.isBlank(tree.getParentId())) {
                rootTrees.add(tree);
            }
            for (LoginMenuVO t : list) {
                if (t.getParentId().equals(tree.getMenuId())) {
                    if (tree.getChildren() == null) {
                        List<LoginMenuVO> myChildren = new ArrayList<>();
                        myChildren.add(t);
                        tree.setChildren(myChildren);
                    } else {
                        tree.getChildren().add(t);
                    }
                }
            }
        }
        return rootTrees;
    }

}
